package com.have.blog.admin.controller;

import com.have.blog.admin.annotation.operationLogger.OperationLogger;
import com.have.blog.admin.domain.dto.AdminDto;
import com.have.blog.admin.service.AdminService;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.security.bo.OnlineAdmin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "管理员相关接口", tags = {"管理员管理接口"})
@RequestMapping("/admin")
@Slf4j
public class AdminAdminController {
    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "获取管理员列表", notes = "获取管理员列表")
    @PostMapping("/getList")
    public Wrapper<CommonPage> getList(@Validated({GetList.class}) @RequestBody AdminDto adminDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        List<AdminDto> list = adminService.getList(adminDto);
        return Wrapper.success(CommonPage.restPage(list));
    }

    @OperationLogger(value = "新增管理员")
    @ApiOperation(value = "新增管理员", notes = "新增管理员")
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody AdminDto adminDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        return adminService.add(adminDto);
    }

    @OperationLogger(value = "编辑管理员")
    @ApiOperation(value = "编辑管理员", notes = "编辑管理员")
    @PostMapping("/edit")
    public Wrapper edit(@Validated({Update.class}) @RequestBody AdminDto adminDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        return adminService.edit(adminDto);
    }

    @OperationLogger(value = "重置用户密码")
    @ApiOperation(value = "重置用户密码", notes = "重置用户密码")
    @PostMapping("/restPwd")
    public Wrapper restPwd(@Validated({Update.class}) @RequestBody AdminDto adminDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return adminService.resetPwd(adminDto);
    }


    @OperationLogger(value = "批量删除管理员")
    @ApiOperation(value = "批量删除管理员", notes = "批量删除管理员")
    @PostMapping("/delete")
    public Wrapper delete(@ApiParam(name = "adminUids", value = "管理员uid集合", required = true) @RequestBody List<Integer> adminUids) {
        return adminService.deleteBatchAdmin(adminUids);
    }

    @ApiOperation(value = "获取在线管理员列表", notes = "获取在线管理员列表", response = String.class)
    @PostMapping(value = "/getOnlineAdminList")
    public Wrapper<CommonPage> getOnlineAdminList(@Validated({GetList.class}) @RequestBody AdminDto adminDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        List<OnlineAdmin> onlineAdminList = adminService.getOnlineAdminList(adminDto);
        return Wrapper.success(CommonPage.restPage(onlineAdminList));
    }

    @OperationLogger(value = "强退用户")
    @ApiOperation(value = "强退用户", notes = "强退用户", response = String.class)
    @PostMapping(value = "/forceLogout")
    public Wrapper forceLogout(@ApiParam(name = "tokenUidList", value = "tokenList", required = false) @RequestBody List<String> tokenUidList) {
        return adminService.forceLogout(tokenUidList);
    }
}
